<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>So Just What is a Policy Anyway?</title>
<link rel="stylesheet" href="../../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Math Toolkit 4.2.1">
<link rel="up" href="../pol_tutorial.html" title="Policy Tutorial">
<link rel="prev" href="../pol_tutorial.html" title="Policy Tutorial">
<link rel="next" href="policy_tut_defaults.html" title="Policies Have Sensible Defaults">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../pol_tutorial.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../pol_tutorial.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="policy_tut_defaults.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="math_toolkit.pol_tutorial.what_is_a_policy"></a><a class="link" href="what_is_a_policy.html" title="So Just What is a Policy Anyway?">So Just What
      is a Policy Anyway?</a>
</h3></div></div></div>
<p>
        A policy is a compile-time mechanism for customising the behaviour of a special
        function, or a statistical distribution. With Policies you can control:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            What action to take when an error occurs.
          </li>
<li class="listitem">
            What happens when you call a function that is mathematically undefined
            (for example, if you ask for the mean of a Cauchy distribution).
          </li>
<li class="listitem">
            What happens when you ask for a quantile of a discrete distribution.
          </li>
<li class="listitem">
            Whether the library is allowed to internally promote <code class="computeroutput"><span class="keyword">float</span></code>
            to <code class="computeroutput"><span class="keyword">double</span></code> and <code class="computeroutput"><span class="keyword">double</span></code> to <code class="computeroutput"><span class="keyword">long</span>
            <span class="keyword">double</span></code> in order to improve precision.
          </li>
<li class="listitem">
            What precision to use when calculating the result.
          </li>
</ul></div>
<p>
        Some of these policies could arguably be run-time variables, but then we
        couldn't use compile-time dispatch internally to select the best evaluation
        method for the given policies.
      </p>
<p>
        For this reason a Policy is a <span class="emphasis"><em>type</em></span>: in fact it's an
        instance of the class template <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">policy</span><span class="special">&lt;&gt;</span></code>. This class is just a compile-time-container
        of user-selected policies (sometimes called a type-list).
      </p>
<p>
        Over a dozen <a class="link" href="policy_tut_defaults.html" title="Policies Have Sensible Defaults">policy
        defaults</a> are provided, so most of the time you can ignore the policy
        framework, but you can overwrite the defaults with your own policies to give
        detailed control, for example:
      </p>
<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">;</span>

<span class="comment">// Define a policy that sets ::errno on overflow,</span>
<span class="comment">// and does not promote double to long double internally,</span>
<span class="comment">// and only aims for precision of only 3 decimal digits,</span>
<span class="comment">// to an error-handling policy, usually to trade precision for speed:</span>

<span class="keyword">typedef</span> <span class="identifier">policy</span>
<span class="special">&lt;</span>
  <span class="identifier">domain_error</span><span class="special">&lt;</span><span class="identifier">errno_on_error</span><span class="special">&gt;,</span>
  <span class="identifier">promote_double</span><span class="special">&lt;</span><span class="keyword">false</span><span class="special">&gt;,</span>
  <span class="identifier">digits10</span><span class="special">&lt;</span><span class="number">3</span><span class="special">&gt;</span>
<span class="special">&gt;</span> <span class="identifier">my_policy</span><span class="special">;</span>
</pre>
</div>
<div class="copyright-footer">Copyright © 2006-2021 Nikhar Agrawal, Anton Bikineev, Matthew Borland,
      Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno
      Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde,
      Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle
      Walker and Xiaogang Zhang<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../pol_tutorial.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../pol_tutorial.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="policy_tut_defaults.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
